﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Model.User;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        LabelError.Visible = false;
        bool tryLogin = true;

        if (TextBoxEmail.Text == "")
        {
            LabelErrorEmail.Visible = true;
            tryLogin = false;
        }
        else
            LabelErrorEmail.Visible = false;

        if (TextBoxPassword.Text == "") 
        {
            LabelErrorPassword.Visible = true;
            tryLogin = false;
        }
        else
            LabelErrorPassword.Visible = false;

        if (tryLogin)
        {
            Person p = AdministrationController.login(TextBoxEmail.Text, TextBoxPassword.Text);

            if (p != null)
            {
                Session["Person"] = p.getId();
                Session["LastAccess"] = DateTime.Now.ToString();
                Response.Redirect("~/CourseView/ViewCoursesLogged.aspx");
            }
            else
            {
                LabelError.Visible = true;
            }
        }
    }
}